<?php


namespace app\app\controller;

use app\common\controller\Homebase;
use EasyWeChat\Factory;
use think\Db;
use app\app\model\Order;
use app\app\model\BkSq;
use app\app\model\Board;

class Shop extends Baseapp
{
    protected $token;
    protected $memberInfo;
    protected $memberId;
    protected $shop_id;
    protected $shop_info;

    protected function initialize()
    {
        parent::initialize();
        $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
        if (empty($authorization)) {
            responseJson(-100, '请先登录');
        }
        $token = trim($authorization);
        $member = Db::name('member')->where('access_token', '=', $token)->find();
        if (!$member) {
            responseJson(-200, '登录信息有误');
        }

        $shop = Db::name('shop')
            ->where('member_id', '=', $member['id'])
            ->find();
        if (!$shop) {
            msg(-300, '店铺账号不存在', '');
        }

        if ($shop['status'] != 1) {
            msg(-400, '店铺账号异常', '');
        }

        //工号限制
        $shop_gh=Db::name('shop_gh')->where([
            ['shop_id','=',$shop['id']],
            ['is_del','=',0]
        ])->find();
        if (!$shop_gh){
            msg(-500, '工号未添加，请联系客服', '');
        }

        $this->token = $token;
        $this->memberInfo = $member;
        $this->memberId = $member['id'];

        $this->shop_info = $shop;
        $this->shop_id = $shop['id'];
    }

    //获取账号信息
    public function getMemberInfo()
    {
        $return_data = $this->appEncrypt($this->memberInfo);
        msg(0, '查询成功', $return_data);
    }

    public function getShopInfo()
    {
        $return_data = $this->appEncrypt($this->shop_info);
        msg(0, '查询成功', $return_data);
    }

    //首页数据统计
    public function indexData1()
    {
        $date1 = getTimestamp(0);
        $date2 = getTimestamp();

        $num1 = Db::name('order')->where([
            ['shop_id', '=', $this->shop_id],
            ['create_time', 'between', [$date1[0], $date1[1]]]
        ])->count();

//        $num2 = Db::name('order')->where([
//            ['shop_id', '=', $this->shop_id],
//            ['create_time', 'between', [$date2[0], $date2[1]]]
//        ])->count();
        $num2 = Db::name('member_finance')->where([
            ['shop_id', '=', $this->shop_id],
            ['role', '=', 2],
            ['type', 'in', [2]]
        ])->sum('money');

        $return_data['a'] = $num1;
        $return_data['b'] = $num2;
        $return_data['c'] = $this->shop_info['money'];
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询完成', $return_data, '');
    }

    //未审核数量 驳回数量
    public function indexData2()
    {
        $num1 = Db::name('order')->where([
            ['status', '=', 0],
            ['shop_id', '=', $this->shop_id]
        ])->count();

        $num2 = Db::name('order')->where([
            ['status', '=', 2],
            ['shop_id', '=', $this->shop_id]
        ])->count();

        $return_data['a'] = $num1;
        $return_data['b'] = $num2;
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询完成', $return_data, '');
    }

    //首页 未审核数量列表 已驳回
    public function indexOrder()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;
//        $page = input('page/d', 1);
//        $length = input('limit/d', 20);

        $where[] = ['shop_id', '=', $this->shop_id];
        //待审核 已驳回
        if ($data['type'] == 1) {
            $where[] = ['status', '=', 0];
        }
        if ($data['type'] == 2) {
            $where[] = ['status', '=', 2];
        }
        $data_list = Order::where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //开卡
    public function openSim()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        //todo 增加异常报警 过滤 sim 卡设置
        //todo 上线关闭在线卡板库存检测

//                $shop_gh=Db::name('shop_gh')->where([
//            ['shop_id','=',$this->shop_id],
//            ['is_del','=',0]
//        ])->find();
//        if (!$shop_gh){
//            msg(-500, '工号不存在，请联系后台', '');
//        }

        if ($this->shop_info['bk_num'] <= 0) {
            msg(-2, '卡板数量不足', '');
        }
        $is_phone = Db::name('order')->where([
            ['phone', '=', $data['phone']],
        ])->find();
        if ($is_phone) {
            msg(-3, '当前手机号已存在', '');
        }
        Db::startTrans();
        try {
            $insert_data = array(
                'order_no' => GetOrderSN(2),
                'salesperson_id' => $this->shop_info['salesperson_id'],
                'shop_id' => $this->shop_id,
                'people' => $data['people'],
                'phone' => $data['phone'],
                'phone2' => $data['phone2'],
                'sim' => $data['sim'],
                'sf_id' => $data['sf_id'],//身份证号
                //手写签名
                'sx_img' => $data['sx_img'],
                'hm_img' => $data['hm_img'],
                'rz_img' => $data['rz_img'],
                'cns_img' => $data['cns_img'],
                'cns_type' => $data['cns_type'],
                'create_time' => time()
            );
            $order_id = Db::name('order')->insertGetId($insert_data);
            //减少白卡库存 增加订单
            Db::name('shop')->where([
                'id' => $this->shop_id
            ])->update([
                'order_num' => Db::raw('order_num + 1'),
                'bk_num' => Db::raw('bk_num - 1')
            ]);
            Db::commit();
            msg(0, '提交完成', $order_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //审核失败编辑开卡信息
    public function editSimOrder()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);

        Db::startTrans();
        try {
            $update_data = array(
                'id' => $data['order_id'],

                'people' => $data['people'],
                'phone' => $data['phone'],
                'phone2' => $data['phone2'],
                'sim' => $data['sim'],
                'sf_id' => $data['sf_id'],//身份证号

                //手写签名
                'sx_img' => $data['sx_img'],
                'hm_img' => $data['hm_img'],
                'rz_img' => $data['rz_img'],
                'cns_img' => $data['cns_img'],
                'cns_type' => $data['cns_type'],

                'kf_status' => 0,
                'kf_status_time' => '',
                'kf_refuse' => '',
                'cw_status' => 0,
                'cw_status_time' => '',
                'cw_refuse' => '',
                'status' => 0
            );
            $order_id = Db::name('order')->update($update_data);
            Db::commit();
            msg(0, '提交完成', $data['order_id']);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //开卡订单列表
    public function orderList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);

        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;
        //0全部 1待审核 2已审核 3未通过
        $where[] = ['shop_id', '=', $this->shop_id];
        $type = $data['type'];
        if (isset($type) && $type) {
            if ($type == 1) {
                $where[] = ['status', '=', 0];
            }
            if ($type == 2) {
                $where[] = ['status', '=', 1];
            }
            if ($type == 3) {
                $where[] = ['status', '=', 2];
            }
        }
//        dump($data);
//        die;

        if (isset($data['datetimerange']) && $data['datetimerange']) {
            $data['datetimerange'] = explode(',', $data['datetimerange']);
            $where[] = ['create_time', 'between', [strtotime($data['datetimerange'][0] . '00:00:00'), strtotime($data['datetimerange'][1] . '23:59:59')]];
        }

//        switch ($type) {
//            case 1:
//                $where[] = ['kf_status', '=', 0];
//                $where[] = ['cw_status', '=', 0];
//                break;
//            case 2:
//                $where[] = ['kf_status', '=', 1];
//                $where[] = ['cw_status', '=', 1];
//                break;
//            case 3:
//                $where[] = ['kf_status|cw_status', '=', 2];
//                break;
//            default:
//                $where[] = ['kf_status', '=', 0];
//                $where[] = ['cw_status', '=', 0];
//        }
        if (isset($data['sno']) && $data['sno']) {
//            $where[] = ['phone|sim', 'like', '%' . $data['sno']];
            $where[] = ['phone', 'like', '%' . $data['sno'] . '%'];
        }

        $data_list = Order::where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                $shop_info = Db::name('shop')->where('id', '=', $this->shop_id)->find();
                $item['shop_info'] = $shop_info;

                $package = Db::name('package')->find($item['package_id']);
                $item['package_name'] = $package['name'];

                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //订单详情
    public function orderDetail()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $order_detail = Order::where('id', '=', $data['id'])->find();

        $package_info = Db::name('package')
            ->where('id', '=', $order_detail['package_id'])
            ->find();
        $shop_info = Db::name('shop')
            ->where('id', '=', $order_detail['shop_id'])
            ->find();
        $order_detail['package_info'] = $package_info;
        $order_detail['shop_info'] = $shop_info;

        //订单流程数组
        //0 待审核 1已审核 2已拒绝
        $li_array = array(
            [
                'text' => '订单提交',
                'create_time' => $order_detail['create_time'],
                'status' => 1,
                'refuse' => '',
            ],
        );

        if ($order_detail['kf_status'] != 0) {
            $li_array[] = array(
                'text' => '客服审核',
                'create_time' => $order_detail['kf_status_time'],
                'status' => $order_detail['kf_status'],
                'refuse' => $order_detail['kf_refuse'],
            );
        } else {
            $li_array[] = array(
                'text' => '客服审核中',
                'create_time' => '',
                'status' => $order_detail['kf_status'],
                'refuse' => '',
            );
        }
        if ($order_detail['kf_status'] == 1) {
            if ($order_detail['cw_status'] != 0) {
                $li_array[] = array(
                    'text' => '财务审核',
                    'create_time' => $order_detail['cw_status_time'],
                    'status' => $order_detail['cw_status'],
                    'refuse' => $order_detail['cw_refuse'],
                );
            } else {
                $li_array[] = array(
                    'text' => '财务审核中',
                    'create_time' => '',
                    'status' => $order_detail['cw_status'],
                    'refuse' => '',
                );
            }
        }

        $order_detail['li_array'] = $li_array;

        $order_detail = $this->appEncrypt($order_detail);
        msg(0, '查询成功', $order_detail);
    }

    //白卡库存数量
    public function boardNum()
    {
//        $board_num = Db::name('board')->where([
//            ['shop_id', '=', $this->shop_id],
//            ['status', '=', 0],
//            ['is_del', '=', 0]
//        ])->count();
        $shop_info = Db::name('shop')->find($this->shop_id);
        $board_num = $this->appEncrypt($shop_info['bk_num']);
        msg(0, '查询成功', $board_num);
    }

    //白卡列表
    public function boardList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;

        $where[] = ['shop_id', '=', $this->shop_id];
        $where[] = ['is_del', '=', 0];
        $where[] = ['hb_time', '>', 0];
        //type 1未开卡 2已开卡
        $type = $data['type'];
        if ($type == 1) {
            $where[] = ['status', '=', 0];
        }
        if ($type == 2) {
            $where[] = ['status', '=', 1];
        }
        if (isset($data['sim']) && $data['sim']) {
            $where[] = ['sim', 'like', '%' . $data['sim'] . '%'];
        }

        $data_list = Board::where($where)
            ->page($page, $length)
            ->order('hb_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                if ($item['status'] == 1) {
                    $order_info = Order::where('sim', '=', $item['sim'])
                        ->field('people,create_time,phone')
                        ->find();
                    $item['order_info'] = $order_info;
                }
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //申请白卡
    public function applyBoard()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        Db::startTrans();
        try {
            $insert_data = array(
                'shop_id' => $this->shop_id,
                'num' => $data['num'],
                'create_time' => time(),
                'status' => 0,
                'province' => $this->shop_info['province'],
                'city' => $this->shop_info['city'],
                'area' => $this->shop_info['area'],
                'address' => $this->shop_info['address'],
                'people' => $this->shop_info['people'],
                'phone' => $this->shop_info['phone'],
            );
            $insert_id = Db::name('bk_sq')->insertGetId($insert_data);
            Db::commit();
            msg(0, '申请完成', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //白卡申请记录
    public function applyList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;

        $where[] = ['shop_id', '=', $this->shop_id];
        //type 0全部 1处理中 2已调拨 3已拒绝
        //status
        $type = $data['type'];
        if ($type == 1) {
            $where[] = ['status', '=', 0];
        }
        if ($type == 2) {
            $where[] = ['status', 'in', [1, 3]];
        }
        if ($type == 3) {
            $where[] = ['status', '=', 2];
        }

        $data_list = BkSq::where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
//                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //白卡申请详情
    public function applyDetail()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $return_data = BkSq::where('id', '=', $data['id'])->find();

        if (in_array($return_data['status']['value'], [0])) {
            $lc_array = array(
                ['text' => '白卡申请',
                    'color' => 'rgba(51, 51, 51, 1)'
                ],
                ['text' => '等待处理',
                    'color' => 'rgba(2, 133, 221, 1)'
                ],
                ['text' => '调拨白卡',
                    'color' => 'rgba(51, 51, 51, 1)'
                ]
            );
        } elseif (in_array($return_data['status'], [2])) {
            $lc_array = array(
                ['text' => '白卡申请',
                    'color' => 'rgba(51, 51, 51, 1)'
                ],
                ['text' => '等待处理',
                    'color' => 'rgba(51, 51, 51, 1)'
                ],
                ['text' => '拒绝',
                    'color' => 'rgba(224, 41, 49, 1)'
                ]
            );
        } else {
            $lc_array = array(
                ['text' => '白卡申请',
                    'color' => 'rgba(51, 51, 51, 1)'
                ],
                ['text' => '等待处理',
                    'color' => 'rgba(51, 51, 51, 1)'
                ],
                ['text' => '调拨白卡',
                    'color' => 'rgba(2, 133, 221, 1)'
                ]
            );
        }
        $return_data['lc_array'] = $lc_array;
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询成功', $return_data);
    }

    //账户中心
    public function accountShop()
    {
        //可提现
        $money = $this->shop_info['money'];
        //待入账
        $dai_money = Db::name('order')->where([
            ['shop_id', '=', $this->shop_id],
            ['status', '=', 0],
        ])->value('reward1 + commission1');
        if (!$dai_money) {
            $dai_money = 0;
        }
        //提现中
        //1业务员到账 2商家到账 2业务提现 4商家提现 2业务扣款 4商家扣款
        $txing_money = Db::name('member_finance')->where([
            ['shop_id', '=', $this->shop_id],
            ['type', '=', 4],
            ['is_count', '=', 2],
            ['is_kuan', '=', 1]
        ])->sum('dz_money');
        //累计提现
        $tx_money = Db::name('member_finance')->where([
            ['shop_id', '=', $this->shop_id],
            ['type', '=', 4],
            ['is_count', '=', 2],
            ['is_kuan', '=', 2]
        ])->sum('dz_money');

        $return_data['a'] = $money;
        $return_data['b'] = $dai_money;
        $return_data['c'] = $txing_money;
        $return_data['d'] = $tx_money;
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询成功', $return_data);
    }

    //提现记录
    public function txList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);

        $where[] = ['shop_id', '=', $this->shop_id];
        $where[] = ['type', '=', 4];

        $rq = $data['rq']; //2025-09
        $riqi = getMonthTimestampRange($rq);

        $where[] = ['create_time', 'between', [$riqi['start_timestamp'], $riqi['end_timestamp']]];

        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;

        $type = $data['type']; //1线上提现 2线下提现
        if ($type == 1) {
            $where[] = ['tx_type', '=', 1];
        }
        if ($type == 2) {
            $where[] = ['tx_type', '=', 2];
        }

        $data_list = Db::name('member_finance')
            ->where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                if ($item['kuan_time']) {
                    $item['kuan_time'] = date('Y-m-d H:i:s', $item['kuan_time']);
                }
                return $item;
            });

        $tx_money = Db::name('member_finance')->where([
            ['shop_id', '=', $this->shop_id],
            ['create_time', 'between', [$riqi['start_timestamp'], $riqi['end_timestamp']]],
            ['type', '=', 4],
            ['is_count', '=', 2],
        ])->sum('money');
        $return_data['data'] = $data_list;
        $return_data['tx_money'] = $tx_money;
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询成功', $return_data);
    }

    //当月已提现金额
    public function nowTx()
    {
        $data = monthStrtotime();
        $tx_money = Db::name('member_finance')->where([
            ['shop_id', '=', $this->shop_id],
            ['create_time', 'between', [$data[0], $data[1]]],
            ['type', '=', 4],
            ['is_count', '=', 2],
        ])->sum('money');
        $return_data['a'] = $this->shop_info['money'];
        $return_data['b'] = $tx_money;
        $return_data = $this->appEncrypt($return_data);
        msg(0, '查询成功', $return_data);
    }

    //共提现金额
    public function txMoneyShop()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);

        $rq = $data['rq']; //2025-09
        $riqi = getMonthTimestampRange($rq);

        $tx_money = Db::name('member_finance')->where([
            ['shop_id', '=', $this->shop_id],
            ['create_time', 'between', [$riqi[0], $riqi[1]]],
            ['type', '=', 4],
            ['is_count', '=', 2],
        ])->sum('money');
        $return_data = $this->appEncrypt($tx_money);
        msg(0, '查询成功', $return_data);
    }

    //余额提现
    public function shopTx()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        //todo 增加不可提现设置
        if ((!$this->shop_info['bank_people'])||(!$this->shop_info['bank_no'])||(!$this->shop_info['bank_name'])){
            msg(-4, '请输入银行信息', '');
        }

        if ($data['money'] <= 0) {
            msg(-1, '输入正确值', '');
        }
        if ($data['money'] > $this->shop_info['money']) {
            msg(-2, '提现金额不能大于余额', '');
        }
        //获取当月微信已提现金额
        $month_data = monthStrtotime();
        $tx_money = Db::name('member_finance')->where([
            ['shop_id', '=', $this->shop_id],
            ['create_time', 'between', [$month_data[0], $month_data[1]]],
            ['type', '=', 4],
            ['tx_type', '=', 1],
            ['is_count', '=', 2],
        ])->sum('money');
        //每月可线上提现金额
        $shop_tx_money = config('site.shop_tx_money');
        //线上可提现金额
        $kt_money = $shop_tx_money - $tx_money;
        if ($data['money'] > $kt_money) {
            $shop_tx_fl = config('site.shop_tx_fl');
            if ($kt_money > 0) {
                //微信提现
                $member_finance_id = setMoney($this->memberId, 0, $this->shop_id, $kt_money, 2, 0, 0, 1, 4);
                //第三方提现
                $tx2_money = round($data['money'] - $kt_money, 2);
                $member_finance_id = setMoney($this->memberId, 0, $this->shop_id, $tx2_money, 2, 0, $shop_tx_fl, 2, 4);
            } else {
                $member_finance_id = setMoney($this->memberId, 0, $this->shop_id, $data['money'], 2, 0, $shop_tx_fl, 2, 4);
            }

        } else {

            $member_finance_id = setMoney($this->memberId, 0, $this->shop_id, $data['money'], 2, 0, 0, 1, 4);
        }
        msg(0, '提现成功', $member_finance_id);
    }

    //修改商家信息
    public function editShopInfo()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);

        Db::startTrans();
        try {
            $update_data = array(
                'id' => $this->shop_id,
            );
            if (isset($data['shop_img']) && $data['shop_img']) {
                $update_data['shop_img'] = $data['shop_img'];
            }
            if (isset($data['shop_name']) && $data['shop_name']) {
                $update_data['shop_name'] = $data['shop_name'];
            }
            $update_id = Db::name('shop')->update($update_data);
            Db::commit();
            msg(0, '设置完成', $update_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //订单奖励  不用
    public function gdJlList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;
        // 0全部 1待审核 2已审核 3未通过
        // status 审核状态 0待审核 1已审核 2审核失败
        $where[] = ['shop_id', '=', $this->shop_id];
        $type = $data['type'];
        if (isset($type) && $type) {
            if ($type == 1) {
                $where[] = ['status', '=', 0];
            }
            if ($type == 2) {
                $where[] = ['status', '=', 1];
            }
            if ($type == 3) {
                $where[] = ['status', '=', 2];
            }
        }
        $data_list = Order::where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //申请卡板收货
    public function kbSh()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $bk_sq_info = Db::name('bk_sq')
            ->where('id', '=', $data['bk_sq_id'])
            ->find();
        if (!$bk_sq_info) {
            msg(-1, '请求不存在', '');
        }
        if ($bk_sq_info['status'] != 1) {
            msg(-2, '当前请求未发货', '');
        }
        Db::startTrans();
        try {
            //修改信息
            Db::name('bk_sq')->update([
                'id' => $data['bk_sq_id'],
                'status' => 3,
                'sh_time' => time()
            ]);
            //卡板设置
            Db::name('board')->where([
                'bk_sq_id' => $data['bk_sq_id'],
                'is_del' => 0
            ])->update([
                'hb_time' => time()
            ]);
            $bk_num = Db::name('board')->where([
                'bk_sq_id' => $data['bk_sq_id'],
                'is_del' => 0
            ])->count();
            //商家卡板增加
            $update_data = Db::name('shop')
                ->where('id', '=', $bk_sq_info['shop_id'])
                ->setInc('bk_num', $bk_num);
            Db::commit();
            msg(0, '完成收货', $update_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //公告列表
    public function noticeList()
    {
        $data = $this->request->param();

        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', '=', 1];

        $data_list = Db::name('notice')
            ->where($where)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    //获取通知详情
    public function noticeDetail()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $notice_info = Db::name('notice')->find($data['notice_id']);
        $notice_info['create_time'] = date('Y-m-d H:i:s', $notice_info['create_time']);
        $return_data = $this->appEncrypt($notice_info);
        msg(0, '查询成功', $return_data);
    }


    //未读消息
    public function wdMsg()
    {
        $num = Db::name('msg')->where([
            ['role', '=', 3],
            ['is_del', '=', 0],
            ['is_read', '=', 0],
            ['member_id', '=', $this->memberId],
        ])->count();
        msg(0, '查询成功', $num);
    }

    //消息清零
    public function ql()
    {
        Db::startTrans();
        try {
            Db::name('msg')->where([
                ['role', '=', 3],
                ['is_del', '=', 0],
                ['member_id', '=', $this->memberId],
            ])->update([
                'is_read' => 1,
                'read_time' => time()
            ]);
            Db::commit();
            msg(0, '清除完成成功', '');
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //店铺聊天记录
    public function shopMsgList1()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 9999999;

        $where[] = ['notice_id', '=', 0];
        $where[] = ['role', 'in', [2, 3]];
        $where[] = ['obj_id', '=', $this->shop_id];
        $where[] = ['member_id', '=', $this->memberId];

        Db::startTrans();
        try {
            $data_list = Db::name('msg')
                ->where($where)
                ->page($page, $length)
                ->order('create_time')
                ->paginate(
                    $length, false, [
                        'page' => $page
                    ]
                )->each(function ($item) {
                    if ($item['role'] == 2) {
                        $item['role'] = 1;
                    }
                    $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                    return $item;
                });
            Db::name('msg')->where($where)->update([
                'is_read' => 1,
                'read_time' => time()
            ]);
            $return_data = $this->appEncrypt($data_list);
            Db::commit();
            msg(0, '查询成功', $return_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //店铺联系平台发送消息
    // role 1用户 2商户 3平台
    public function sendMsgShop()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        Db::startTrans();
        try {
            $insert_id = sendMsg(2, $this->memberId, $data['content'], $this->shop_id);
            Db::commit();
            msg(0, '发送完成', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //获取通知发送消息列表
    public function shopMsgList2()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 9999999;

        $where[] = ['notice_id', '=', $data['notice_id']];
        $where[] = ['role', 'in', [2, 3]];
        $where[] = ['obj_id', '=', $this->shop_id];
        $where[] = ['member_id', '=', $this->memberId];
        Db::startTrans();
        try {
            $data_list = Db::name('msg')
                ->where($where)
                ->page($page, $length)
                ->order('create_time')
                ->paginate(
                    $length, false, [
                        'page' => $page
                    ]
                )->each(function ($item) {
                    if ($item['role'] == 2) {
                        $item['role'] = 1;
                    }
                    $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                    return $item;
                });
            Db::name('msg')->where($where)->update([
                'is_read' => 1,
                'read_time' => time()
            ]);
            $return_data = $this->appEncrypt($data_list);
            Db::commit();
            msg(0, '查询成功', $return_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //发送关于通知的消息
    public function send2MsgShop()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        Db::startTrans();
        try {
            $insert_id = sendMsg(2, $this->memberId, $data['content'], $this->shop_id, $data['notice_id']);
            Db::commit();
            msg(0, '发送完成', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //设置账户信息 手机号
    public function setShopInfo()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        Db::startTrans();
        try {
            $update_info = array(
                'id' => $this->shop_id,
            );
            if (isset($data['phone']) && $data['phone']) {
                $update_info['phone'] = $data['phone'];
//                Db::name('member')->update([
//                    'id' => $this->memberId,
//                    'tx_phone' => $data['phone']
//                ]);
            }

            if (isset($data['bank_name']) && $data['bank_name']) {
                $update_info['bank_name'] = $data['bank_name'];
            }
            if (isset($data['bank_people']) && $data['bank_people']) {
                $update_info['bank_people'] = $data['bank_people'];
            }
            if (isset($data['bank_no']) && $data['bank_no']) {
                $update_info['bank_no'] = $data['bank_no'];
            }
            Db::name('shop')->update($update_info);
            Db::commit();
            msg(0, '设置完成', '');
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    public function setPassword(){
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        Db::startTrans();
        try {
            $password=encrypt_password(trim($data['password']), $this->memberInfo['salt']);
            if ($password!=$this->memberInfo['password']){
                msg(-2, '密码错误', '');
            }
            $new_password=encrypt_password(trim($data['new_password']), $this->memberInfo['salt']);
            Db::name('member')->update([
                'id'=>$this->memberId,
                'password'=>$new_password
            ]);
            msg(0, '设置完成', '');
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //首页数据统计
    public function indexOrderList()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 20;


        $date1 = getTimestamp(0);
        $date2 = getTimestamp();
        $where[]=['shop_id', '=', $this->shop_id];
        switch ($data['search_type']) {
            case 1:
                $where[]=['create_time', 'between', [$date1[0], $date1[1]]];
                break;
            case 2:
                $where[]=['create_time', 'between', [$date2[0], $date2[1]]];
                break;
            default:
                $where[]=['create_time', 'between', [$date1[0], $date1[1]]];
        }
        $data_list = Order::where($where)
            ->page($page, $length)
            ->order('create_time desc')
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                return $item;
            });
        $return_data = $this->appEncrypt($data_list);
        msg(0, '查询成功', $return_data);
    }

    public function setShopInfo2()
    {
        $data = $this->request->param();
        $data = $this->appDecrypt($data);
        Db::startTrans();
        try {
            $update_info = array(
                'id' => $this->shop_id,
            );
            if (isset($data['people']) && $data['people']) {
                $update_info['people'] = $data['people'];
            }
            if (isset($data['sf_img1']) && $data['sf_img1']) {
                $update_info['sf_img1'] = $data['sf_img1'];
            }
            if (isset($data['sf_img2']) && $data['sf_img2']) {
                $update_info['sf_img2'] = $data['sf_img2'];
            }
            if (isset($data['shop_name']) && $data['shop_name']) {
                $update_info['shop_name'] = $data['shop_name'];
            }

            if (isset($data['province']) && $data['province']) {
                $update_info['province'] = $data['province'];
            }
            if (isset($data['city']) && $data['city']) {
                $update_info['city'] = $data['city'];
            }
            if (isset($data['area']) && $data['area']) {
                $update_info['area'] = $data['area'];
            }
            if (isset($data['address']) && $data['address']) {
                $update_info['address'] = $data['address'];
            }
            if (isset($data['mp']) && $data['mp']) {
                $update_info['mp'] = $data['mp'];
            }

            if (isset($data['shop_img']) && $data['shop_img']) {
                $update_info['shop_img'] = $data['shop_img'];
            }
            if (isset($data['jq_img']) && $data['jq_img']) {
                $update_info['jq_img'] = $data['jq_img'];
            }
            if (isset($data['rz_img']) && $data['rz_img']) {
                $update_info['rz_img'] = $data['rz_img'];
            }

            if (isset($data['bank_name']) && $data['bank_name']) {
                $update_info['bank_name'] = $data['bank_name'];
            }
            if (isset($data['bank_people']) && $data['bank_people']) {
                $update_info['bank_people'] = $data['bank_people'];
            }
            if (isset($data['bank_no']) && $data['bank_no']) {
                $update_info['bank_no'] = $data['bank_no'];
            }
            Db::name('shop')->update($update_info);
            Db::commit();
            msg(0, '设置完成', '');
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }


}